using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using Pampa.Common.Utils;

namespace Pampa.WebControls
{
    [ToolboxData("<{0}:DatePickerCombos runat=server></{0}:DatePickerCombos>")]
    [DesignerAttribute(typeof(BaseDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class DatePickerCombos : BaseControl
    {
        DateTime? date;
        int yearFrom;
        int yearTo;
        string pathImages;
        bool showYear;

        public DatePickerCombos()
        {
            this.CssClass = "datePickerCombosDefault";
            this.clientClass = "DatePickerCombos";
            this.yearFrom = 1920;
            this.yearTo = DateTime.Today.Year;
            this.pathImages = "Pampa.WebControls" + GetImagesPath();
        }

        #region Properties

        public DateTime? Date
        {
            get { return date; }
            set { date = value; }
        }

        public int YearFrom
        {
            get { return yearFrom; }
            set { yearFrom = value; }
        }

        public int YearTo
        {
            get { return yearTo; }
            set { yearTo = value; }
        }

        public string PathImages
        {
            get { return pathImages; }
            set { pathImages = value; }
        }

        public bool ShowYear
        {
            get { return showYear; }
            set { showYear = value; }
        }

        private string ObjectName
        {
            get
            {
                return "dp" + this.ID.Replace(".", "_");
            }
        }

        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.CssClass == "datePickerCombosDefault")
                RegisterStyleSheet("Pampa.WebControls/DatePickerCombos.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.WebControls/FocusManager.js");
            RegisterLibrary("Pampa.WebControls/DatePickerCombos.js");
            RegisterLibrary("Pampa.WebControls/ComboList.js");

            // recupero el valor tomado en el Customer
            if (Page.IsPostBack)
            {
                LoadValue();
            }
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", "cont_" + this.ID);
            writer.AddAttribute("class", this.CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            RenderDefinitions(writer);
        }

        /// <summary>
        /// Construyo el objeto Customer que representa al combo
        /// </summary>
        private void RenderDefinitions(HtmlTextWriter output)
        {
            output.WriteLine("<script type=\"text/javascript\">");
            output.WriteLine("var " + this.ObjectName + " = new " + this.clientClass + "(\"" + this.ID + "\");");

            // Properties                
            RenderProperties(output);

            output.WriteLine(this.ObjectName + ".show(\"cont_" + this.ID + "\", \"" + GetResourceUrl(this.PathImages) + "\");");
            output.WriteLine("</script>");
        }

        protected virtual void RenderProperties(HtmlTextWriter output)
        {
            string objectName = this.ObjectName;

            output.WriteLine(objectName + ".dateFormat = \"" + Formats.DateFormat + "\";"); 
            output.WriteLine(objectName + ".cssClass = \"" + this.CssClass + "\";");
            output.WriteLine(objectName + ".yearFrom = " + this.YearFrom.ToString() + ";");
            output.WriteLine(objectName + ".yearTo = " + this.YearTo.ToString() + ";");
            output.WriteLine(objectName + ".enabled = " + this.Enabled.ToString().ToLower() + ";");
            output.WriteLine(objectName + ".showYear = " + this.ShowYear.ToString().ToLower() + ";");

            if(!this.ShowYear)
            {
                var names = string.Join(",", CultureInfo.CurrentCulture.DateTimeFormat.MonthNames); 
                output.WriteLine(objectName + ".names = \"" + names + "\";");
            }

            // Seteo la fecha del calendar
            if (this.Date.HasValue)
            {
                output.WriteLine(objectName + ".year = " + this.Date.Value.Year.ToString() + ";");
                output.WriteLine(objectName + ".month = " + this.Date.Value.Month.ToString() + ";");
                output.WriteLine(objectName + ".day = " + this.Date.Value.Day.ToString() + ";");
            }

            if (!this.Enabled)
            {
                output.WriteLine(objectName + ".disable();");  
            }
        }

        #endregion

        #region Recover Client Data

        private void LoadValue()
        {
            if (!(String.IsNullOrEmpty(Page.Request[this.ID])))
            {
                // TODO
            }
        }

        #endregion
    }
}
